import {Injectable} from "@angular/core";
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import {Storage} from "@ionic/storage";

@Injectable()
export class HistoryService {

  constructor(private storage: Storage) {
  }

  query(): Promise<Map<string,any>>{
    return this.storage.get("history").then(res => {
      return res;
    }).catch(err => {
      console.error(err);
      return err;
    });
  }

  /**
   * 将历史记录放入storage中,
   * todo 集合最大100条记录
   * @param article
   */
  put(article: any) {
    this.storage.get("history").then(res => {
      if (!res) {
        res = new Map();
      }
      res.set(article.code, article);
      this.storage.set('history', res);
    }).catch(err => {
      console.log("记录历史失败 " + err)
    })
  }
}
